<?php

namespace app\v1\controller;

use support\Request;
use app\service\CartService;
use Exception;

/**
 * 购物车控制器（使用Eloquent ORM和服务类）
 * 
 * @package app\v1\controller
 */
class CartControllerEloquent
{
    protected $cartService;

    public function __construct(CartService $cartService)
    {
        $this->cartService = $cartService;
    }

    /**
     * 添加到购物车
     * 
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        try {
            // 参数验证
            $goodsId = $request->post('goods_id');
            $spec = $request->post('spec');
            $quantity = $request->post('quantity', 1);
            $userId = $request->post('user_id');

            if (empty($goodsId)) {
                throw new Exception('商品ID不能为空');
            }

            if (empty($spec)) {
                throw new Exception('商品规格不能为空');
            }

            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            if ($quantity <= 0) {
                throw new Exception('商品数量必须大于0');
            }

            // 使用服务类处理业务逻辑
            $result = $this->cartService->addToCart($userId, $goodsId, $spec, $quantity);

            return json([
                'data' => $result,
                'msg' => '添加到购物车成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取购物车列表
     * 
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        try {
            $userId = $request->get('user_id');
            
            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            // 使用服务类获取购物车列表
            $result = $this->cartService->getUserCartList($userId);

            return json([
                'data' => $result,
                'msg' => '获取购物车列表成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 更新购物车商品数量
     * 
     * @param Request $request
     * @return mixed
     */
    public function updateQuantity(Request $request)
    {
        try {
            $cartId = $request->post('cart_id');
            $quantity = $request->post('quantity');
            
            if (empty($cartId)) {
                throw new Exception('购物车记录ID不能为空');
            }
            
            if ($quantity <= 0) {
                throw new Exception('商品数量必须大于0');
            }

            // 使用服务类更新数量
            $result = $this->cartService->updateQuantity($cartId, $quantity);

            return json([
                'data' => $result,
                'msg' => '更新数量成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除购物车商品
     * 
     * @param Request $request
     * @return mixed
     */
    public function remove(Request $request)
    {
        try {
            $cartId = $request->post('cart_id');
            
            if (empty($cartId)) {
                throw new Exception('购物车记录ID不能为空');
            }

            // 使用服务类删除商品
            $result = $this->cartService->removeFromCart($cartId);

            return json([
                'data' => $result,
                'msg' => '删除成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 清空购物车
     * 
     * @param Request $request
     * @return mixed
     */
    public function clear(Request $request)
    {
        try {
            $userId = $request->post('user_id');
            
            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            // 使用服务类清空购物车
            $result = $this->cartService->clearUserCart($userId);

            return json([
                'data' => $result,
                'msg' => '清空购物车成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取购物车商品总数
     * 
     * @param Request $request
     * @return mixed
     */
    public function count(Request $request)
    {
        try {
            $userId = $request->get('user_id');
            
            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            // 使用服务类获取商品总数
            $count = $this->cartService->getUserCartCount($userId);

            return json([
                'data' => [
                    'count' => $count
                ],
                'msg' => '获取购物车商品总数成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 批量操作购物车商品
     * 
     * @param Request $request
     * @return mixed
     */
    public function batchOperation(Request $request)
    {
        try {
            $userId = $request->post('user_id');
            $cartIds = $request->post('cart_ids', []);
            $action = $request->post('action');

            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            if (empty($cartIds)) {
                throw new Exception('请选择要操作的商品');
            }

            if (empty($action)) {
                throw new Exception('操作类型不能为空');
            }

            // 使用服务类进行批量操作
            $result = $this->cartService->batchOperation($userId, $cartIds, $action);

            return json([
                'data' => $result,
                'msg' => $result['message'],
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取购物车统计信息
     * 
     * @param Request $request
     * @return mixed
     */
    public function stats(Request $request)
    {
        try {
            $userId = $request->get('user_id');
            
            if (empty($userId)) {
                throw new Exception('用户ID不能为空');
            }

            // 使用服务类获取统计信息
            $stats = $this->cartService->getCartStats($userId);

            return json([
                'data' => $stats,
                'msg' => '获取购物车统计信息成功',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
}
